﻿using System.IO;
using System.IO.Compression;
using System.Web;

namespace CY_MVC.Utility
{
    public static class HttpGzip
    {
        public static bool CanGZip()
        {
            var acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            return !string.IsNullOrEmpty(acceptEncoding) && (acceptEncoding.Contains("gzip"));
        }

        public static byte[] GzipStr(byte[] p_Bytes)
        {
            using (var stream = new MemoryStream())
            {
                using (var writer = new GZipStream(stream, CompressionMode.Compress))
                {
                    writer.Write(p_Bytes, 0, p_Bytes.Length);
                }
                p_Bytes = stream.ToArray();
            }
            return p_Bytes;
        }
    }
}
